package com.handpay.zztong.hp;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.protocol.BaseProtocolKey;

/**
 * 获取银联签购单页面
 * 
 * @author cqli 2013.3.29
 *
 */
public class SignaturePhone extends ZZTongActivity implements OnClickListener{
	private EditText editView = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signature_phone);
		setClickEvent();
		editView = (EditText) findViewById(R.id.editPhone);
	}

	private void setClickEvent() {
		findViewById(R.id.btn_number0).setOnClickListener(this);
		findViewById(R.id.btn_number1).setOnClickListener(this);
		findViewById(R.id.btn_number2).setOnClickListener(this);
		findViewById(R.id.btn_number3).setOnClickListener(this);
		findViewById(R.id.btn_number4).setOnClickListener(this);
		findViewById(R.id.btn_number5).setOnClickListener(this);
		findViewById(R.id.btn_number6).setOnClickListener(this);
		findViewById(R.id.btn_number7).setOnClickListener(this);
		findViewById(R.id.btn_number8).setOnClickListener(this);
		findViewById(R.id.btn_number9).setOnClickListener(this);
		findViewById(R.id.btn_numberBack).setOnClickListener(this);
		findViewById(R.id.btn_numberCancle).setOnClickListener(this);
		findViewById(R.id.btn_numberSend).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		String path  = v.getResources().getResourceName(v.getId()); 
		String vName = path.substring(path.indexOf("/")+1); 
		vName = vName.replace("btn_number", "");
		if(TextUtils.isDigitsOnly(vName)){
            editView.getRootView().invalidate();
			editView.append(vName);
			editView.getRootView().invalidate();
		}else if(vName.equals("Back")){
			String text = editView.getText().toString();
			if(text.length() > 0){
				editView.setText(text.substring(0,text.length()-1));
				editView.setSelection(text.length()-1);
			}
		}else if(vName.equals("Cancle")){
			setResult(Activity.RESULT_CANCELED, getIntent());
			finish();
		}else if(vName.equals("Send")){
			String phone = editView.getText().toString();
			if (TextUtils.isEmpty(phone)) {
				Toast.makeText(this, R.string.siguture_phone, Toast.LENGTH_SHORT).show();
			} else if (!CommonUtils.isMobileNum(phone)) {
				Toast.makeText(this, R.string.input_right_phone_tip, Toast.LENGTH_SHORT).show();
			} else 
			{
				getIntent().putExtra(BaseProtocolKey.PHONE, phone);
				this.setResult(Activity.RESULT_OK, getIntent());
				finish();
			}
		}
		
	}
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if(KeyEvent.KEYCODE_BACK==keyCode)
            return false ;
        return super.onKeyDown(keyCode, event);
    }
    @Override
    protected boolean isShownTopBar() {
        return false;
    }

    @Override
    protected boolean isShownTabs() {
        return false;
    }
}
